﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Security.Permissions;
using System.Web.Services.Description;
using System.Collections.Generic;
using System.Web.Configuration;

public partial class MasterPage : System.Web.UI.MasterPage
{
    //khoi tao ket noi csdl
    static string strconn = WebConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
    SqlConnection con = new SqlConnection(strconn);
    public string language = "vi";
    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {

            language = Session["lang"].ToString();
        }
        catch (Exception) { }

        if (!IsPostBack)
        {
            
            //String url = this.Page.Request.Url.AbsoluteUri;
            String url = this.Page.Request.Url.AbsolutePath;

            Basic.Replace util = new Basic.Replace();
            
            if (!url.Contains("lang="))
            {
                if (!url.Contains(".aspx?"))
                {
                    imbVietNam.PostBackUrl = ResolveUrl(url) + "?lang=vi";
                    imbEnglish.PostBackUrl = ResolveUrl(url) + "?lang=en";
                   
                }
                else
                {
                    imbVietNam.PostBackUrl = ResolveUrl(url) + "&lang=vi";
                    imbEnglish.PostBackUrl = ResolveUrl(url) + "&lang=en";
                   
                }
            }
            else
            {
                if (url.Contains("lang=vi"))
                {
                    imbEnglish.PostBackUrl = ResolveUrl(util.ReplaceLanguage(url, "lang=vi", "lang=en"));
                   
                }
                if (url.Contains("lang=en"))
                {
                    imbVietNam.PostBackUrl = ResolveUrl(util.ReplaceLanguage(url, "lang=en", "lang=vi"));
                   
                }
                
            }
            
        }
                

    }
    protected void Login1_Authenticate(object sender, AuthenticateEventArgs e)
    {

       

    }
    public int LoginCheck(string username, string password)
    {
        con.Open();
        SqlCommand authen = new SqlCommand("select TenDangNhap, MatKhau,LoaiTK from ThanhVien where TenDangNhap=@tendangnhap and MatKhau=@matkhau", con);
        SqlParameter user = new SqlParameter("@tendangnhap", username.Trim());
        SqlParameter pass = new SqlParameter("@matkhau",password.Trim());

        authen.Parameters.Add(user);
        authen.Parameters.Add(pass);

        SqlDataReader reader = authen.ExecuteReader();
        if (reader.HasRows)
        {
            reader.Read();
            int kq = int.Parse(reader.GetString(2));
            con.Close();
            return kq;
        }
        con.Close();
        return 0;
    }
    protected void LinkButton1_Click(object sender, EventArgs e)
    {

        // khai bao cookie 
        HttpCookie cookie_username = new HttpCookie("username", "");
        //Thoi gian song cua cookie
        cookie_username.Expires = DateTime.Now;
        // add Cookie
        Response.Cookies.Add(cookie_username);
        HttpCookie cookie_type = new HttpCookie("LoaiTK", "");
        cookie_type.Expires = DateTime.Now;
        Response.Cookies.Add(cookie_type);

        String url = this.Page.Request.Url.AbsolutePath;
        if (url.Contains("lang=vi"))
        {
            Response.Redirect("Home.aspx?lang=vi");
        }
        else
        {
            Response.Redirect("Home.aspx?lang=en");
        }
    }
    protected void LoginButton_Click(object sender, EventArgs e)
    {
         String url = this.Page.Request.Url.AbsolutePath;
        int log = LoginCheck(UserName.Text.Trim(), Password.Text.Trim());
        if (log == 0)
        {
            if (url.Contains("lang=vi"))
            {
                FailureText.Text = "Đăng Nhập thất bại!";
            }
            else
            {
                FailureText.Text = "Login Fail";
            }
            return;
        }
        HttpCookie cookie_user = new HttpCookie("username", UserName.Text.Trim());
        cookie_user.Expires = DateTime.Now.AddDays(10);
        Response.Cookies.Add(cookie_user);
        HttpCookie cookie_type = new HttpCookie("LoaiTK", log.ToString().Trim());
        cookie_type.Expires = DateTime.Now.AddDays(5);
        Response.Cookies.Add(cookie_type);
        if (log == 1)
        {
            if (url.Contains("lang=vi"))
            {
                Response.Redirect("AdminPage.aspx?lang=vi");
            }
            else
            {
                Response.Redirect("AdminPage.aspx?lang=en");
            }
        }
        else
            Response.Redirect("Home.aspx");
    }
}
